package com.stonedt.api.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
/**
 * 数据api接口
 * 注意替换Authorization
 * @author wangyi
 *
 */
public class HttpAPIUtil {
	public static void main(String[] args) {
		
		
		//测试招投标列表数据
		String sendPostEsSearch = sendPostEsSearch("http://data.stonedt.com/api/invitationOftenderlist","starttime=2021-01-01 00:00:00&keyword=南京");
		System.out.println(sendPostEsSearch);
		
		
	}
       public static String sendPostEsSearch(String url, String params) {
		
		try {
			PrintWriter out = null;
			BufferedReader in = null;
			URL realUrl = new URL(url);
			URLConnection conn = realUrl.openConnection();
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
			//此处赋值通过TokenUtil工具类返回的token的数据
			conn.setRequestProperty("Authorization", "A3238D8BD028FFA0E3A00D55EB7BE4D9");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			out = new PrintWriter(conn.getOutputStream());
			out.print(params);
			out.flush();
			in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			StringBuilder response = new StringBuilder();
			String line;
			while ((line = in.readLine()) != null) {
				response.append(line);
			}
			try {
				if (out != null) {
					out.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
			return response.toString();
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

}
